package _10_binary_tree;

/**
 * @author: mornd
 * @dateTime: 2023/6/11 - 14:36
 */
public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    @Override
    public String toString() {
        return String.valueOf(val);
    }


    /**
     * 打印树结构
     * @return
     */
    public String printTreeStructure() {
        StringBuilder sb = new StringBuilder();
        toStringHelper(this, "", "", sb);
        return sb.toString();
    }

    private void toStringHelper(TreeNode node, String prefix, String childPrefix, StringBuilder sb) {
        if (node == null) {
            sb.append(prefix).append("null\n");
            return;
        }

        sb.append(prefix).append(node.val).append("\n");
        toStringHelper(node.left, childPrefix + "├── ", childPrefix + "│   ", sb);
        toStringHelper(node.right, childPrefix + "└── ", childPrefix + "    ", sb);
    }
}
